Pygame collision of two rectangles

by: Bobby264, 9 years ago

Last edited: 9 years ago

EDIT: I solved all my problems no need to anwser this question


Hello,
can you tell me why the console throws an Attribute Error at me when trying to detect collision between those two rectangles

<pre class='prettyprint lang-py'>
class Snake():
    def __init__(self, screen, startx, starty, width, height, color):
        self.screen = screen
        self.startx = startx
        self.starty = starty
        self.width = width
        self.height = height
        self.color = color
        self.amount_tiles = 1
        self.tile = 0
        self.position = []
        self.rect = pygame.Rect(startx, starty, width, height)
        pygame.draw.rect(screen, color, self.rect)
</pre>

<pre class='prettyprint lang-py'>
def existing_tiles(self):
        self.count = self.tile

        while self.count > 0:
            self.current_pos = self.position
            x = self.current_pos[self.count - 1]
            self.rect_new = pygame.Rect(x[0], x[1], self.width, self.height)
            pygame.draw.rect(self.screen, GREEN, self.rect_new)
            self.count = self.count - 1
</pre>

It's a snake game and I tryied to detect collision between the head of the snake and his tail like this:

<pre class='prettyprint lang-py'>
if pygame.Rect.colliderect(rect_1, rect_2) == True:
</pre>






You must be logged in to post. Please login or register an account.



existing_tiles is part of the Snake Class

In case you are wondering :^)

-Bobby264 9 years ago
Last edited 9 years ago

You must be logged in to post. Please login or register an account.